package com.gxuwz.dao.impl;

import com.gxuwz.dao.StudentDao;
import com.gxuwz.entity.Student;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.entity.vo.StudentVo;
import com.gxuwz.result.PageInfo;
import com.gxuwz.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class StudentDaoImpl implements StudentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public PageVo selectStudentAll(Student entity, PageInfo pageInfo,String instID) {
        String sql = "select stuID,stuName,sex,className,address,stuTel,contact,contactTel " +
                "from sys_student,sys_classes,sys_instructor where sys_student.classID = sys_classes.classID " +
                "and sys_student.del_flag = 0 and sys_instructor.instID = sys_classes.instID " +
                "and sys_instructor.instID = ? and sys_classes.del_flag = 0 and sys_instructor.del_flag = 0 ";
        BeanPropertyRowMapper<StudentVo> departmentBeanPropertyRowMapper = new BeanPropertyRowMapper<StudentVo>(StudentVo.class);
        List<Object> example = new ArrayList<Object>();
        PageUtil pageUtil = new PageUtil();
        if(entity==null){
            Object[] params = new Object[]{instID};
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, departmentBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        } else {
            Object[] params;
            example.add(instID);
            if(entity.getStuID() != null && !entity.getStuID().equals("")){
                sql += "and stuID like ?";
                example.add("%"+entity.getStuID()+"%");
            }
            if(entity.getStuName() != null && !entity.getStuName().equals("")) {
                sql += "and stuName like ?";
                example.add("%"+entity.getStuName()+"%");
            }
            if(entity.getSex() != null && !entity.getSex().equals("")) {
                sql += "and sex like ?";
                example.add("%"+entity.getSex()+"%");
            }
            if(entity.getStuTel() != null && !entity.getStuTel().equals("")){
                sql += "and stuTel like ?";
                example.add("%"+entity.getStuTel()+"%");
            }
            System.out.println(sql);
            params = example.toArray();
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, departmentBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        }
    }

    public Student selectStudentByID(String stuID) {
        String sql = "select * from sys_student where stuID = ? and del_flag = 0";
        BeanPropertyRowMapper<Student> departmentBeanPropertyRowMapper = new BeanPropertyRowMapper<Student>(Student.class);
        Object[] params = new Object[]{stuID};
        try{
            Student student = jdbcTemplate.queryForObject(sql, departmentBeanPropertyRowMapper, params);
            return student;
        } catch (Exception ex){
            return null;
        }
    }

    public int insertStudent(Student entity) {
        String sql = "insert into sys_student values(?,?,?,?,?,?,?,?,?,0)";
        Object[] params = new Object[]{
                entity.getStuID(),entity.getClassID(),
                entity.getPassword(),entity.getStuName(),
                entity.getSex(),entity.getAddress(),
                entity.getStuTel(),entity.getContact(),
                entity.getContactTel()
        };
        int insert = jdbcTemplate.update(sql, params);
        return insert;
    }

    public int updateStudent(Student entity, String oldId) {
        String sql = "update sys_student set classID = ? , password = ?," +
                "stuName = ?,sex = ?,address = ?,stuTel = ?,contact = ?,contactTel = ? where stuID = ?";
        Object[] params = new Object[]{
                entity.getClassID(),entity.getPassword(),
                entity.getStuName(),entity.getSex(),
                entity.getAddress(),entity.getStuTel(),
                entity.getContact(),entity.getContactTel(),
                oldId
        };
        int update = jdbcTemplate.update(sql, params);
        return update;
    }

    public int deleteStudentById(String stuID) {
        String sql = "update sys_student set del_flag = 1 where stuID = ?";
        Object[] params = new Object[]{stuID};
        int delete = jdbcTemplate.update(sql, params);
        return delete;
    }

    public int deleteStudents(String[] ids) {
        int count = 0;
        String sql = "update sys_student set del_flag = 1 where stuID = ?";
        for (int i = 0; i < ids.length; i++){
            Object[] params = new Object[]{ids[i]};
            int delete = jdbcTemplate.update(sql, params);
            count += delete;
        }
        return count;
    }

    @Override
    public List<Student> selectStudentByClassesID(String classId) {
        String sql = "select * from sys_student where classID = ? and del_flag = 0 ";
        BeanPropertyRowMapper<Student> studentBeanPropertyRowMapper = new BeanPropertyRowMapper<>(Student.class);
        Object[] params = new Object[]{classId};
        List<Student> query = jdbcTemplate.query(sql, studentBeanPropertyRowMapper, params);
        return query;
    }

}
